<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.4: http://docutils.sourceforge.net/" />
<title>I18n Implementation</title>
<style type="text/css">

/*
:Author: David Goodger
:Contact: goodger@users.sourceforge.net
:Date: $Date: 2005-12-18 01:56:14 +0100 (Sun, 18 Dec 2005) $
:Revision: $Revision: 4224 $
:Copyright: This stylesheet has been placed in the public domain.

Default cascading style sheet for the HTML output of Docutils.

See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
customize this style sheet.
*/

/* used to remove borders from tables and images */
.borderless, table.borderless td, table.borderless th {
  border: 0 }

table.borderless td, table.borderless th {
  /* Override padding for "table.docutils td" with "! important".
     The right padding separates the table cells. */
  padding: 0 0.5em 0 0 ! important }

.first {
  /* Override more specific margin styles with "! important". */
  margin-top: 0 ! important }

.last, .with-subtitle {
  margin-bottom: 0 ! important }

.hidden {
  display: none }

a.toc-backref {
  text-decoration: none ;
  color: black }

blockquote.epigraph {
  margin: 2em 5em ; }

dl.docutils dd {
  margin-bottom: 0.5em }

/* Uncomment (and remove this text!) to get bold-faced definition list terms
dl.docutils dt {
  font-weight: bold }
*/

div.abstract {
  margin: 2em 5em }

div.abstract p.topic-title {
  font-weight: bold ;
  text-align: center }

div.admonition, div.attention, div.caution, div.danger, div.error,
div.hint, div.important, div.note, div.tip, div.warning {
  margin: 2em ;
  border: medium outset ;
  padding: 1em }

div.admonition p.admonition-title, div.hint p.admonition-title,
div.important p.admonition-title, div.note p.admonition-title,
div.tip p.admonition-title {
  font-weight: bold ;
  font-family: sans-serif }

div.attention p.admonition-title, div.caution p.admonition-title,
div.danger p.admonition-title, div.error p.admonition-title,
div.warning p.admonition-title {
  color: red ;
  font-weight: bold ;
  font-family: sans-serif }

/* Uncomment (and remove this text!) to get reduced vertical space in
   compound paragraphs.
div.compound .compound-first, div.compound .compound-middle {
  margin-bottom: 0.5em }

div.compound .compound-last, div.compound .compound-middle {
  margin-top: 0.5em }
*/

div.dedication {
  margin: 2em 5em ;
  text-align: center ;
  font-style: italic }

div.dedication p.topic-title {
  font-weight: bold ;
  font-style: normal }

div.figure {
  margin-left: 2em ;
  margin-right: 2em }

div.footer, div.header {
  clear: both;
  font-size: smaller }

div.line-block {
  display: block ;
  margin-top: 1em ;
  margin-bottom: 1em }

div.line-block div.line-block {
  margin-top: 0 ;
  margin-bottom: 0 ;
  margin-left: 1.5em }

div.sidebar {
  margin-left: 1em ;
  border: medium outset ;
  padding: 1em ;
  background-color: #ffffee ;
  width: 40% ;
  float: right ;
  clear: right }

div.sidebar p.rubric {
  font-family: sans-serif ;
  font-size: medium }

div.system-messages {
  margin: 5em }

div.system-messages h1 {
  color: red }

div.system-message {
  border: medium outset ;
  padding: 1em }

div.system-message p.system-message-title {
  color: red ;
  font-weight: bold }

div.topic {
  margin: 2em }

h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
  margin-top: 0.4em }

h1.title {
  text-align: center }

h2.subtitle {
  text-align: center }

hr.docutils {
  width: 75% }

img.align-left {
  clear: left }

img.align-right {
  clear: right }

ol.simple, ul.simple {
  margin-bottom: 1em }

ol.arabic {
  list-style: decimal }

ol.loweralpha {
  list-style: lower-alpha }

ol.upperalpha {
  list-style: upper-alpha }

ol.lowerroman {
  list-style: lower-roman }

ol.upperroman {
  list-style: upper-roman }

p.attribution {
  text-align: right ;
  margin-left: 50% }

p.caption {
  font-style: italic }

p.credits {
  font-style: italic ;
  font-size: smaller }

p.label {
  white-space: nowrap }

p.rubric {
  font-weight: bold ;
  font-size: larger ;
  color: maroon ;
  text-align: center }

p.sidebar-title {
  font-family: sans-serif ;
  font-weight: bold ;
  font-size: larger }

p.sidebar-subtitle {
  font-family: sans-serif ;
  font-weight: bold }

p.topic-title {
  font-weight: bold }

pre.address {
  margin-bottom: 0 ;
  margin-top: 0 ;
  font-family: serif ;
  font-size: 100% }

pre.literal-block, pre.doctest-block {
  margin-left: 2em ;
  margin-right: 2em ;
  background-color: #eeeeee }

span.classifier {
  font-family: sans-serif ;
  font-style: oblique }

span.classifier-delimiter {
  font-family: sans-serif ;
  font-weight: bold }

span.interpreted {
  font-family: sans-serif }

span.option {
  white-space: nowrap }

span.pre {
  white-space: pre }

span.problematic {
  color: red }

span.section-subtitle {
  /* font-size relative to parent (h1..h6 element) */
  font-size: 80% }

table.citation {
  border-left: solid 1px gray;
  margin-left: 1px }

table.docinfo {
  margin: 2em 4em }

table.docutils {
  margin-top: 0.5em ;
  margin-bottom: 0.5em }

table.footnote {
  border-left: solid 1px black;
  margin-left: 1px }

table.docutils td, table.docutils th,
table.docinfo td, table.docinfo th {
  padding-left: 0.5em ;
  padding-right: 0.5em ;
  vertical-align: top }

table.docutils th.field-name, table.docinfo th.docinfo-name {
  font-weight: bold ;
  text-align: left ;
  white-space: nowrap ;
  padding-left: 0 }

h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
  font-size: 100% }

tt.docutils {
  background-color: #eeeeee }

ul.auto-toc {
  list-style-type: none }

</style>
</head>
<body>
<div class="document" id="i18n-implementation">
<h1 class="title">I18n Implementation</h1>
<p>: Author: limodou
: Contact: <a class="reference" href="mailto:limodou&#64;gmail.com">limodou&#64;gmail.com</a>
: Home: <a class="reference" href="http://code.google.com/p/ulipad/">http://code.google.com/p/ulipad/</a>
: BLOG: <a class="reference" href="http://www.donews.net/limodou">http://www.donews.net/limodou</a>
: Copyright: FDL</p>
<p>(Note: translated from Chinese Simplified by translate.google.com.  Will be improved over time.)</p>
<div class="contents topic">
<p class="topic-title first"><a id="contents" name="contents">Contents</a></p>
<ul class="simple">
<li><a class="reference" href="#one-the-usual-method" id="id1" name="id1">one, the usual method</a></li>
<li><a class="reference" href="#two-problems-encountered" id="id2" name="id2">two, problems encountered</a></li>
<li><a class="reference" href="#three-the-solution" id="id3" name="id3">three, the solution</a></li>
<li><a class="reference" href="#four-the-specific-use-ulipad" id="id4" name="id4">four, the specific use UliPad</a></li>
<li><a class="reference" href="#of-the-realization-of-gettext" id="id5" name="id5">of: the realization of gettext</a></li>
<li><a class="reference" href="#i18n-problem-of-a-small" id="id6" name="id6">i18n problem of a small</a></li>
</ul>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id1" id="one-the-usual-method" name="one-the-usual-method">one, the usual method</a></h1>
<p>What is i18n, it is the acronym of the international (Internationalization, and I started removing the final N, among a total of 18 characters). For its realization,
In Python documentation on the gettext in detail. While I wxPyWiki wxPyCookbook found on the realization of the international guidelines
I have the documentation and tutorial, I began to experiment.</p>
<p>Example I use handbook provided, but I was revised. First, the addition of Chinese, Spanish and French removed the handle. Second, some of the information on another
Modules, inter-module testing can be achieved. Also in this module will be conducted with international content on the list, such as:</p>
<pre class="literal-block">
Message = [_('English'), _('Chinese')]
</pre>
<p>Now I usually describe a method.</p>
<ol class="arabic">
<li><p class="first">the main module into gettext module, it needs on the international handling
of the statement, modules into before. Such as:</p>
<pre class="literal-block">
Gettext.install('i18ntest', './Locale', unicode=True)
</pre>
<p>Three parameters were meant to:</p>
<ul class="simple">
<li>Role of domain name, the translation of documents for limited overnight</li>
<li>Path, the path translation of documents stored</li>
<li>Unicode, the use of unicode (If your application is unicode, they were here for True)</li>
</ul>
<p>The above instructions will install a default of the international class.
We need to install a specific class of international, we will be able to:</p>
<pre class="literal-block">
Gettext.translation('i18ntest','./Locale',languages=['en']).Install(True)
</pre>
</li>
</ol>
<p>This will install designated translation of documents. Before gettext.install with two parameters, the third parameter of the specified type of language. Will gettext.translation
Return to a new object. Implementation of the install function will be installed to support the international language of the designated processing functions. Install the Senate for the use of unicode.</p>
<ol class="arabic">
<li><p class="first">all need to deal with a string of international add _(), such as: English and Chinese in need of international treatment, then to into: _( 'English')
And _( 'Chinese'). This step may be more troublesome.
After the treatment, your program is still no real translation of documents but still be running.</p>
</li>
<li><p class="first">use pygettext.py strings extraction. Pygettext.py python in the installation package onboard a tool, in tools/i18n Contents
In the meantime, there is a msgfmt.py procedures, to be used for the
translation of documents into gettext identifiable binary files. Command-line
parameters are as follows:</p>
<pre class="literal-block">
Python pygettext.py File Name
</pre>
</li>
</ol>
<p>Both documents were many. The python pygettext.py - help it can be seen what
support options. If you do not specify the output file name, it'll create a file
named messages.pot by default. It is the most primitive to translate
documents into other languages. Open it you will see the following as possible:</p>
<pre class="literal-block">
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR ORGANIZATION
# FIRST AUTHOR &lt;EMAIL&#64;ADDRESS&gt;, YEAR.
#
msgid &quot;&quot;
msgstr &quot;&quot;
&quot;Project-Id-Version: PACKAGE VERSION\n&quot;
&quot;POT-Creation-Date: Mon Jun 14 13:28:26 2004\n&quot;
&quot;PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n&quot;
&quot;Last-Translator: FULL NAME &lt;\n&quot;&gt;EMAIL&#64;ADDRESS&gt;\n&quot;
&quot;Language-Team: LANGUAGE &lt;\n&quot;&gt;LL&#64;li.org&gt;\n&quot;
&quot;MIME-Version: 1.0\n&quot;
&quot;Content-Type: text/plain; charset=CHARSET\n&quot;
&quot;Content-Transfer-Encoding: ENCODING\n&quot;
&quot;Generated-By: pygettext.py 1.5\n&quot;

#: i18ntest.py:28 i18ntest.py:76
msgid &quot;MiniApp&quot;
msgstr &quot;&quot;

#: i18ntest.py:39 i18ntest.py:85
msgid &quot;E&amp;xit&quot;
msgstr &quot;&quot;

A messages.pot comprising the following elements:

*  '#' to start the Notes
*  empty row
*  msgid OK, a translation of that, how can firms. If more firms, such as fractal::

    Msgid &quot;&quot;
    &quot;This is a multiline \ n&quot;
    &quot;Test&quot;
    If msgid content is empty, the document said msgstr information

* msgstr OK, that a translation can be more, the same format msgid. If msgid
  is empty, compared with document information. In the document information
  can be input Commissioner. Some asked this situation, the most important
  of which is charset domain specified in this document for a coded message,
  which is to save the document to be used in the encoding. Chinese proposals
  on the use utf-8 encoding.

Then we get this pot documents, the first of its preservation for the different versions
to the following translation. Such as: messages_cn.po. Translation, only consider
Msgid not empty, was asked to be in the msgstr.
</pre>
<ol class="arabic" start="4">
<li><p class="first">use msgfmt.py document will po mo converted to binary documents. The command line as follows:</p>
<pre class="literal-block">
Python msgfmt.py messages_cn.po
</pre>
<p>This will generate a  messages_cn.mo called the binary files.</p>
</li>
<li><p class="first">installation mo document. Mo is the fact the correct position on the document. Gettext request translation of documents organized into the following directories:</p>
<pre class="literal-block">
./Locale/en/LC_MESSAGES
./Locale/cn/LC_MESSAGES
</pre>
<p>Locale in gettext.translation in designated. En, cn language classification.
LC_MESSAGES is requested. Mo also should read documents and Scope</p>
<p>The same, as i18ntest.mo.</p>
<p>Through the steps above, with a support of the international availability of the
procedure.  In wxPyWiki Guide mki18n.py used in the procedure. But I used the python's
own tools, and guide differentiated.</p>
</li>
</ol>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id2" id="two-problems-encountered" name="two-problems-encountered">two, problems encountered</a></h1>
<p>By the usual methods of some inconvenient place, I feel the following points:</p>
<ol class="arabic simple">
<li>Pygettext.py can read more than one paper, but too many documents, in order not to be easily put aside. Documents do not support directory and list processing.</li>
<li>When I revised the procedures, the need to translate parts of the changes, if new changes to the original content has been translated merger?</li>
<li>Local / en / directory organizations such LC_MESSAGE some trouble.</li>
</ol>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id3" id="three-the-solution" name="three-the-solution">three, the solution</a></h1>
<p>For the first one, my solution is to amend pygettext.py source. I did not realize directory, list of the documents dealt with. First The need to deal with the Python source files generated a document, a document of the party. Then pygettext.py increase in the list of documents from the command line election Xiang. The revised document if interested can download the latest source code UliPad.py (published in this paper, can only download from the cvs).</p>
<p>For the second question, my solution is to prepare a merger. First, it would have translated documents read out by keyword saved to a dictionary. To read the latest generation of translation of documents, translated documents for each of the translation of one, if not in the dictionary there, it will be directly inserted into the dictionary; If so, And asked not to air, then the replacement of the original Yes. Dealing with the new translation of documents, then remove dictionary keywords in the new translation of documents in the non-existent. This Points may have to pay attention to, because I always creating new translation of documents is a complete collection, I use this method to remove unwanted items translation. If the new inverted Translation document is not complete collection, removal should be removed from the dictionary keyword operation. But if removal of unwanted translation of the manual to be completed. This procedure also UliPad in the source code.</p>
<p>For the third question, I gettext.py access to the sourcecode. Find out why in () function. It finally, the need to generate a mo file name, as follows:</p>
<pre class="literal-block">
Mofile = os.path.join(localedir, lang, 'LC_MESSAGES', '%s.mo'%domain)
</pre>
<p>Mo can see a document by the name of the directory, the type of language, LC_MESSAGES, and the role of the domain name. Form:</p>
<pre class="literal-block">
Localedir/lang/LC_MESSAGES/domain.mo
</pre>
<p>Therefore, the need to establish this as the corresponding directory, and mo files on the corresponding directory.</p>
<p>My idea is: all the translation of documents on the same directory (if large projects involving many different matter on the translation process), Document Translation: overnight _ region coding. Mo. Why not encoding language? Because, as English and Chinese are the difference between English and language are en coding, From this fundamental distinction. If regional coding can be, respectively cn and tw.</p>
<p>So in order to achieve this role, I prepared the gettext own i18n model in achieving the gettext function also satisfied on the basis of my request. While this Modules can also operate under the default locale automatically called the corresponding translation of documents. With this document in UliPad source code.</p>
<blockquote>
Thus, in UliPad with i18n related elements:</blockquote>
<ul class="simple">
<li>pygettext.py(Modified)</li>
<li>Msgfmt.py</li>
<li>I18n.py (of gettext function)</li>
<li>Mergepo.py (Merger po language)</li>
</ul>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id4" id="four-the-specific-use-ulipad" name="four-the-specific-use-ulipad">four, the specific use UliPad</a></h1>
<p>UliPad in the start-up procedures - UliPad. Py around in front of Accession:</p>
<pre class="literal-block">
1    sys.path.append('./modules')
2    import i18n
3    i18n = i18n.I18n('UliPad', './lang', unicode=True)
4    try:
5        import Lang
6    except:
7        pass
8    else:
9        i18n.install(Lang.language)
</pre>
<p>I18n is on modules in the directory. The third generation of a i18n object, it calls the same parameters gettext.install. This runs to the third line will be Called automatically default translation of documents. The default document translation: from the default system locale, be in code, and area code judgment match the translation of documents Here, if there is load; If it does not exist, not the use of any translation of documents, so the system of the international treatment.</p>
<p>5 trip into Lang module. It is in fact, I used a small tricks. Users can specify the type of language, if designated completed, will be in modules directory Lang.py generate a document, which reads: language = &quot;language.&quot; That is the language of the user selected area code to preserve this document to the next Startup called corresponding language documents. Therefore, users UliPad changes in the language, only to resume the new language settings will take effect.</p>
<p>Into i18n only need to be initiated into the module, other modules do not have to import.</p>
<p>Then can be translated in all areas to join _ () (fore work).</p>
<p>Then generate a list of documents.</p>
<p>Call pygettext.py UliPad_cn.po generation language documents.</p>
<p>Right UliPad_cn.po translation.</p>
<p>Call msgfmt.py to compile UliPad_cn.po to mo file.</p>
<p>Will UliPad_cn.mo lang files are copied to the directory.</p>
<p>Start UliPad, OK, basically not a problem.</p>
<p>If the source code to be amended, then called pygettext.py generate new translation of documents messages.pot. Call mergepo.py (with language) the new and old Fusion. Mo to generate documents, copies of the directory lang, so cycle.  UliPad but there are also a problem: how to deal with the resources xml document.</p>
<p>In UliPad, like search, search and replace, text, text - including several features of this dialog are using the resources of the xml document, is not As general as to the source _ () to handle. My method is to generate resources in different languages xml documents, the need to use resources in the document, in accordance with the procedures selected The language, called the corresponding resource files. Because resources are xml document, the text encoding with the encoding xml consistent statement. The Chinese inverted Translation of documents, the proposed use utf-8 encoding. Because utf-8 is the default xml use encoding.</p>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id5" id="of-the-realization-of-gettext" name="of-the-realization-of-gettext">of: the realization of gettext</a></h1>
<p>Is my understanding!</p>
<p>Gettext function actually quite simple: the definition _ () function, and installation, according to the translation of the introduction of a document from the mo asked, and the logo applied uniocde The unicode encrypted.</p>
<p>A gettext clients access to the two basic functions:: gettext and ugettext. No one used to be asked transcoding, with a Unicode to be transformed into asked. Unicode under a sign that one of the two functions will be used to _ () function on. If not install any Yes, back to the default Translation is the only back, returned unchanged. Do not install any translation, gettext same can be used.</p>
<p>How to install _ ().</p>
<pre class="literal-block">
import __buildin__
__buildin__.__dict__['_']=unicode and self.ugettext or self.gettext
</pre>
<p>This will be _ () function into a built-in functions, in the follow-up to the other modules can directly use it. Therefore, the use of gettext, installation to be very on
and load it before the starup module</p>
<p>Postscript: Why not dynamically updated interface UliPad</p>
<p>Since the realization of the international  treatment, if you change the use of the language, if not immediately changed the entire application better? Of course very attrĺactive, but UliPad Problems. On the one hand, all interface with the international handling of the relevant local, should use the code to achieve renewal process, workload. In addition, unless the test, a user Not always in various languages to switch to switch between, are generally fixed to a certain language, therefore, re-start after the opening of a new set of language is acceptable Program. There is one important reason: the use of _ () function in this way are only suitable for calling, the global variable or variables module powerless. Why? For example:</p>
<pre class="literal-block">
Message = _('Chinese')
</pre>
<p>That defines a variable. If it is the existence of a module, module as a global variable so that when the introduction of this module, this statement will be implemented. And I. The process will be implemented first. When this statement after the message is that the implementation of specific value, and is no longer message = _ ( 'Chinese') Such a statement. So, When we change the language, the message is only one language translation, not a new translation. This is really very difficult to solve. And why in the function call Can? Strictly speaking this should be in the form of:</p>
<pre class="literal-block">
Call (_ ( 'Chinese'))
</pre>
<p>This is because when the call is a dynamic function, parameter calculation, therefore, call on every invocation will be re-calculated parameter's value, so that way we can. General to Said that if the implementation of each can call _ () function, this can be achieved Dynamic language switching. If not, use static language switching. Therefore UliPad Use of the static language switching.</p>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id6" id="i18n-problem-of-a-small" name="i18n-problem-of-a-small">i18n problem of a small</a></h1>
<p>Is not only I found that the use of UliPad other people also found that sometimes in the Shell window some statement and then return to the document window, with the mouse Subject mobile text automatically selected, or even buttons are non-functional. This issue is very interesting, why he had ordered the Shell will work? Later, I found that this Because of the use of the i18n _ () function and Shell _ in the conflict caused by variable. In Shell, a command End implementation, the results on the preservation of order in _ variable. But i18n this' _ 'use of a built-in function, thus creating the two conflicts. So I had to UliPad of all _ () function has changed all Tr () (define), Now before we can solve this problem. To illustrate, PyShell (use it generated Shell window) and the variable space is UliPad Overlap. Probably not, since the i18n _ () function is built through its placement in __builtin__, and the overall situation is __builtin__ share, So it is bound to affect changes to other modules.</p>
<p>Fortunately, this problem can be solved. But PyShell seem not to support Chinese. More than originally wanted to achieve some features, but bring some negative impact carefully is the best oh.</p>
<p><cite>_</cite> [Return]</p>
<!-- _ `[Return]`: technical.htm -->
</div>
</div>
</body>
</html>
